package cn.com.cym.gamezao.web;

import cn.com.cym.gamezao.annotation.AuthToken;
import cn.com.cym.gamezao.annotation.CommonApi;
import cn.com.cym.gamezao.base.ResultResponse;
import cn.com.cym.gamezao.base.SearchPage;
import cn.com.cym.gamezao.entity.Dict;
import cn.com.cym.gamezao.entity.DictItem;
import cn.com.cym.gamezao.exception.BaseException;
import cn.com.cym.gamezao.service.DictItemService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "x-字典对应数据")
@RestController
@RequestMapping(value = "dictItem")
@CommonApi
public class DictItemController {

    final DictItemService thisService;

    public DictItemController(DictItemService dictItemService){
        this.thisService=dictItemService;
    }


    @ApiImplicitParam(name = "编码", value = "code", required = true,dataType = "String")
    @ApiOperation(value = "按照字典编码获取")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @GetMapping("/{code}")
    public ResponseEntity<?> getByCode(@PathVariable String code){
        DictItem dictItem = this.thisService.getByCode(code);
        return ResponseEntity.ok(dictItem);
    }

    @ApiImplicitParam(name = "编码", value = "code", required = true,dataType = "String")
    @ApiOperation(value = "按照字典编码获取详细数据")
    @ApiOperationSupport(order = 2, author = "mr.chen")
    @GetMapping("/dict/{dictId}")
    public ResponseEntity<?> getByDictCode(@PathVariable Long dictId){
        List<DictItem> dictItems = this.thisService.getByDictId(dictId);
        return ResponseEntity.ok(dictItems);
    }


    @ApiImplicitParam(name = "dictItem", value = "dictItem", required = true,dataType = "DictItem")
    @ApiOperation(value = "新建字典对应数据")
    @ApiOperationSupport(order = 3, author = "mr.chen")
    @PostMapping()
    public ResponseEntity<?> add(@RequestBody DictItem dictItem){
        if(dictItem.getDict()==null){
            throw new BaseException("不存在主表数据，无法新建",new ResultResponse("不存在主表数据，无法新建", HttpStatus.BAD_REQUEST.value()));
        }
        final DictItem storeDictItem = this.thisService.save(dictItem);
        return ResponseEntity.ok(storeDictItem);
    }


    @ApiImplicitParams({@ApiImplicitParam(name = "code", value = "code",paramType = "path", required = true,dataType = "String"),
            @ApiImplicitParam(name = "dictItem", value = "dictItem",paramType = "", required = true,dataType = "DictItem")})
    @ApiOperation(value = "修改字典数据")
    @ApiOperationSupport(order = 4, author = "mr.chen")
    @PutMapping("/{code}")
    public ResponseEntity<?> update(@RequestBody DictItem dictItem,@PathVariable String code){
        if(dictItem.getDict()==null){
            throw new BaseException("不存在主表数据，无法更新",new ResultResponse("不存在主表数据，无法更新", HttpStatus.BAD_REQUEST.value()));
        }
        final DictItem storeDictItem = this.thisService.updateByCode(dictItem,code);
        return ResponseEntity.ok(storeDictItem);
    }

    @ApiImplicitParam(name = "searchPage", value = "searchPage", required = true,dataType = "SearchPage")
    @ApiOperation(value = "分页显示page！")
    @ApiOperationSupport(order = 4, author = "mr.chen")
    @PostMapping("/page")
    @AuthToken
    public ResponseEntity<?> page(@RequestBody SearchPage searchPage){
        Page<?> byPageable = this.thisService.findByPageable(searchPage);
        return ResponseEntity.ok(byPageable);
    }
}
